class InitShaders2 {
    static getShader (gl, shaderScript, type) {
        let shader = gl.createShader(type);

        gl.shaderSource(shader, shaderScript.call());
        gl.compileShader(shader);

        if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
            alert(gl.getShaderInfoLog(shader));
            return null;
        }

        return shader;
    }

    static initShaders(gl, vertexShaderScript, fragmentShaderScript) {
        let program = gl.createProgram();
        let vertexShader = InitShaders2.getShader(gl, vertexShaderScript, gl.VERTEX_SHADER);
        let fragmentShader = InitShaders2.getShader(gl, fragmentShaderScript, gl.FRAGMENT_SHADER);

        gl.attachShader(program, vertexShader);
        gl.attachShader(program, fragmentShader);
        gl.linkProgram(program);

        if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
            alert("Could not initialise shaders");
            return null;
        }
        
        return program;
    };
}

module.exports = InitShaders2;
